package com.example.wsappproject.MvpDemo.mainpage;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.example.wsappproject.MvpDemo.Injection;
import com.example.wsappproject.R;
import com.example.wsappproject.Util.ToastUtil;
import com.example.wsappproject.adapter.ArticleAdapter;
import com.example.wsappproject.practice.ArticleEntity;

import java.util.ArrayList;
import java.util.List;

public class MvpActivity extends AppCompatActivity implements MvpContract.View{

    private Button btn;
    private RecyclerView rv;
    private MvpContract.Presenter mPresenter;
    private ArticleAdapter mArticleAdapter;
    private List<ArticleEntity.DataBean.DatasBean> mArticleList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvp);

        initView();
        initData();
        initEvent();

        mPresenter = new MvpPresenter(
                Injection.ProvideRepository(getApplicationContext()),this);
    }

    private void initView() {
         btn = findViewById(R.id.btn_mvp_article);
         rv = findViewById(R.id.rv_mvp_project_fragment);
    }

    private void initData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(linearLayoutManager);
        mArticleAdapter = new ArticleAdapter(mArticleList,this);
        rv.setAdapter(mArticleAdapter);
    }

    private void initEvent() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtil.showToast(MvpActivity.this,"假装这里有旋转等待框");
                mPresenter.articleList(mCurrentPage);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
    }

    int mCurrentPage = 0;

    @Override
    public void showArticleList(ArticleEntity articleEntity) {
        if (mCurrentPage ==0){
            mArticleList.clear();
        }
        mArticleList.addAll(articleEntity.getData().getDatas());
        mArticleAdapter.notifyDataSetChanged();
    }

    @Override
    public void setPresenter(MvpContract.Presenter Presenter) {

    }
}